package com.shuoyue.ycgk.ui.shop.contract;


import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.ShopCarBean;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.List;

import io.reactivex.Observable;

public interface ShopCarContract {
    class Model extends BaseModel {
        public Observable<BaseResult<List<ShopCarBean>>> getCarList() {
            return RetrofitClient.getInstance().getApi().queryShoppingCarList();
        }

        public Observable<BaseResult<String>> delete(String carId) {
            return RetrofitClient.getInstance().getApi().deleteShoppingCar(carId);
        }

        public Observable<BaseResult<Object>> updateNum(int id, int num) {
            return RetrofitClient.getInstance().getApi().updateShoppingCarByCount(id, num);
        }
    }


    interface View extends BaseView {
        void setData(List<ShopCarBean> list);

        void deleteSuc();

        void updateSuc();
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();


        public void delete(String ids) {
            apply(model.delete(ids))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> stringOptional) {
                            super.onNext(stringOptional);
                            mView.deleteSuc();
                        }
                    });
        }

        public void getData() {
            apply(model.getCarList())
                    .subscribe(new ApiSubscriber<Optional<List<ShopCarBean>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<ShopCarBean>> listOptional) {
                            super.onNext(listOptional);
                            mView.setData(listOptional.getIncludeNull());
                        }
                    });
        }

        public void updateNum(int id, int num) {
            apply(model.updateNum(id, num))
                    .subscribe(new ApiSubscriber<Optional<Object>>(mView, this) {
                        @Override
                        public void onNext(Optional<Object> stringOptional) {
                            super.onNext(stringOptional);
                            mView.updateSuc();
                        }
                    });
        }
    }

}
